/* Copyright (C) 
 * 
 * Disclaim
 * This program is the property of AI Speech Ltd. It shall be communicated to
 * authorized personnel only. It is not to be disclosed outside the group without
 * prior written consent. If you are not sure if you’re authorized to read this
 * program, please contact info@aispeech.com before reading.
 * 
 */


/**
 * @file wtk_utils_array.h
 * 
 * @brief some utility function for wtk_array 
 * 
 * @author wxm71(weixing.mei@aispeech.com)
 * 
 * @version 0.0.1
 * 
 * @date 2014-12-15
 */

#ifndef ___WXM71___AISPEECH_WTK_UTILS_ARRAY__HEADER___
#define ___WXM71___AISPEECH_WTK_UTILS_ARRAY__HEADER___

#include "wtk/core/wtk_array.h"
#include "wtk/core/wtk_strbuf.h"

#ifdef __cplusplus 
extern "C" {
#endif 


// get item in array
#define wtk_utils_array_get_val(arr, idx, type) ( ((type*)arr->slot)[idx] )
#define wtk_utils_array_get_ptr(arr, idx, type) ( ((type*)arr->slot)+(idx) )

// get pointer to a given item(char *)
#define wtk_utils_array_get_addr(arr, idx) (NULL==arr||idx<0||idx>=arr->nslot)? NULL:(char*)(arr->slot+(idx)*arr->slot_size)


// call back for deep copy array item
typedef void*(*wtk_array_cp_handler_t)(void *item, void *data, wtk_heap_t *heap);


/**
 * @brief shallow copy of array 
 */
wtk_array_t* wtk_array_copy(wtk_array_t *src, wtk_heap_t *heap);


/**
 * @brief deep copy of array
 *
 * @param src array to be copy
 * @param hand call back function used to copy array item
 * @param data user data used while copy array item
 * @param heap heap used for create output array
 */
wtk_array_t* wtk_array_deep_copy(wtk_array_t *src, wtk_array_cp_handler_t hand, void *data, wtk_heap_t *heap);


/**
 * @brief add a string to an array of wtk_string_t*, only add when given string is not in array
 *
 * @param arr, array of wtk_string_t* 
 */
void wtk_utils_str_array_uniq_push(wtk_array_t *arr, char *data, int len);
#define wtk_utils_str_array_uniq_push_s(arr, s) wtk_utils_str_array_uniq_push(arr, s, strle(s))
#define wtk_utils_str_array_uniq_push_str(arr, str) wtk_utils_str_array_uniq_push(arr, (str)->data, (str)->len)


/**
 * @brief whether a string appear in a wtk_string_t* array
 *
 * @param arr, wtk_string_t* array
 */
int wtk_utils_str_array_has(wtk_array_t* arr, char *data, int len); 
#define wtk_utils_str_array_has_s(arr, s) wtk_utils_str_array_has(arr, s, strlen(s))
#define wtk_utils_str_array_has_str(arr, str) wtk_utils_str_array_has(arr, (str)->data, (str)->len)

/**
 * @brief join a string array with given seperator
 *
 * @param arr wtk_string_t* array to be join
 * @param sep seperator
 * @param len length of seperator
 * @param buf output buffer
 * @param reset whether reset output buffer before join
 */
void wtk_utils_str_array_join(wtk_array_t* arr, char *sep, int len, wtk_strbuf_t *buf, int reset);

#ifdef __cplusplus  
};
#endif

#endif

